import { Strategy, ExtractJwt } from 'passport-jwt'
import { find } from '../db/dao'
import { USER } from '../db/model'

const opts = {
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey: 'secret'
}

export default new Strategy(opts, function (payload, done) {
  find(USER, {
    _id: payload.id
  }).then(users => {
    if (users) {
      return done(null, users)
    }
    return done(null, false)
  }).catch(err => done(err, false))
})
